import { View } from "../../../mvc/core/View";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { AttachmentItem } from "./AttachmentItem";
import { DataEvent } from "../../events/DataEvent";
import { MailEventName } from "./MailEventName";
import { SMail } from "../../../../Message/Game/SMail";
import { EMailType } from "../../../../Message/Game/EMailType";
import { Language } from "../../../../extend/language/Language";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { Cache } from "../../cache/Cache";
import { MailUtil } from "./MailUtil";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { SPlayerItem } from "../../../../Message/Public/SPlayerItem";
import { ItemData } from "../../resource/info/ItemData";
import { ClickToShowItemTip } from "../common/tooltip/ClickToShowItemTip";
import { ImagesConst } from "../../resource/ImagesConst";
import { GLabel } from "../../../../com/mui/controls/GLabel";
import { ObjCreate } from "../common/ObjCreate";
import { GScrollPane } from "../../../../com/mui/controls/GScrollPane";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { GButton } from "../../../../com/mui/controls/GButton";
import { GTextInput } from "../../../../com/mui/controls/GTextInput";
type int = number;
//class CollectPanel
    
    export  class CollectPanel extends View
    {
        constructor()
        {
            super();
            this._itemsArray = [];
            this.createChildren();
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            return;
        }

        /* internal  */createAttachItem(): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=0;
            while (loc1 < 3) 
            {
                loc2 = GlobalClass.getBitmap("PackItemBg");
                loc2.width = 38;
                loc2.height = 38;
                loc2.x = 34 + loc1 * 43;
                loc2.y = 223;
                this.addChild(loc2);
                loc3 = new AttachmentItem();
                loc3.posIndex = loc1 + 1;
                loc3.setSize(32, 32);
                loc3.move(36 + loc1 * 44, 228);
                loc3.isDragAble = false;
                loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onAttachmentClickHandler);
                this.addChild(loc3);
                this._itemsArray.push(loc3);
                ++loc1;
            }
            return;
        }

        public onAttachmentClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as AttachmentItem;
            if (loc1 && loc1.dragSource) 
            {
                this.dispatchEvent(new DataEvent(MailEventName.GETSINGLEATTA, loc1.posIndex, false, true));
            }
            return;
        }

        public /* override */ update(arg1: Object, ... rest): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=null;
            super.update(arg1, rest);
            this.clearAttachment();
            this._smail = arg1 as SMail;
            this._titleText.text = this._smail.title;
            if (this._smail.type != EMailType._EMailTypePlayer) 
            {
                this._showContent = this._smail.content;
                this._tipTxt.htmlText = Language.getString(52061);
            }
            else 
            {
                this._showContent = HTMLUtil.removeHtml(this._smail.content);
                if (Cache.instance.friend.isFriendByPlayerId(this._smail.fromPlayerId)) 
                {
                    this._tipTxt.htmlText = Language.getString(52059);
                }
                else 
                {
                    this._tipTxt.htmlText = Language.getString(52060);
                }
            }
            this._showContent = MailUtil.getReplaceContent(this._showContent);
            LoaderHelp.addResCallBack(ResFileConst.mail, this.updateBitmap);
            this._addresserText.htmlText = this._smail.fromPlayerName + HTMLUtil.addColor("  " + this._typeName, "#f0ea3f");
            this._contentText.htmlText = "<textFormat leading=\'5\'>" + this._showContent + "</textFormat>";
            this._contentText.drawNow();
            if (this._contentText.textHeight <= 145) 
            {
                this._contentText.height = 145;
            }
            else 
            {
                this._contentText.height = this._contentText.textHeight + 8;
            }
            this._contentPane.validateNow();
            this._contentPane.update();
            this._goldText.text = this._smail.attachmentGold.toString();
            this._coinText.text = this._smail.attachmentCoin.toString();
            if (this._smail.playerItems.length > 0) 
            {
                loc1 = 0;
                while (loc1 < this._smail.playerItems.length) 
                {
                    loc2 = this._itemsArray[loc1] as AttachmentItem;
                    loc3 = this._smail.playerItems[loc1] as SPlayerItem;
                    loc2.dragSource = new ItemData(loc3);
                    ++loc1;
                }
            }
            else 
            {
                loc4 = 0;
                while (loc4 < this._itemsArray.length) 
                {
                    (loc5 = this._itemsArray[loc4] as AttachmentItem).detory();
                    ++loc4;
                }
            }
            return;
        }

        public clearAttachment(): void
        {
            var loc2=null;
            var loc1=0;
            while (loc1 < this._itemsArray.length) 
            {
                if (this._itemsArray[loc1]) 
                {
                    loc2 = this._itemsArray[loc1] as AttachmentItem;
                    loc2.detory();
                }
                ++loc1;
            }
            this._goldText.text = "0";
            this._coinText.text = "0";
            return;
        }

        /* internal  */onLinkTextClickHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=arg1.text;
            if (loc1.indexOf("ItemCode#") != -1) 
            {
                this._isItemTip = true;
                loc2 = loc1.split("#");
                loc3 = parseInt(loc2[1]);
                ClickToShowItemTip.instance.setItemCodeAndShow(loc3);
                return;
            }
            MailUtil.dealMailLinkByType(loc1);
            return;
        }

        /* internal  */updateBitmap(): void
        {
            if (this._smail.type != EMailType._EMailTypePlayer) 
            {
                this._typeBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.SystemMailLabel);
            }
            else 
            {
                this._typeBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.UserMailLabel);
            }
            this._typeBitmap.x = 275 - this._typeBitmap.width;
            return;
        }

        public clearPanel(): void
        {
            this._addresserText.text = "";
            this._titleText.text = "";
            this._contentText.text = "";
            this._typeBitmap.bitmapData = null;
            this._tipTxt.text = "";
            this.clearAttachment();
            return;
        }

        /* internal  */onRemoveFromStageHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._isItemTip) 
            {
                this._isItemTip = false;
                ClickToShowItemTip.instance.hide();
            }
            return;
        }

        public createChildren(): void
        {
            this._addresserLabel = new GLabel();
            this._addresserLabel.htmlText = HTMLUtil.addColor(Language.getString(52000), "#B1F0FF");
            this._addresserLabel.move(13, 7);
            this.addChild(this._addresserLabel);
            this._addresserText = ObjCreate.createGTextInput(62, 7, 220, 20, this);
            this._addresserText.editable = false;
            this._addresserText.name = "addresserText";
            this._typeBitmap = new Object /* flash.display.Bitmap */();
            this._typeBitmap.y = 9;
            this.addChild(this._typeBitmap);
            this._titleLabel = new GLabel();
            this._titleLabel.htmlText = HTMLUtil.addColor(Language.getString(52001), "#B1F0FF");
            this._titleLabel.move(13, 32);
            this.addChild(this._titleLabel);
            this._titleText = ObjCreate.createGTextInput(62, 29, 220, 20, this);
            this._titleText.editable = false;
            this._titleText.name = "titleText";
            ObjCreate.createBg(0, 52, 295, 20, this, ImagesConst.SelectedBg);
            this._tipTxt = ObjCreate.createGTextField("", 0, 53, 220, 20, this);
            this._tipTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._contentPane = new GScrollPane();
            this._contentPane.styleName = "GScrollPane";
            this._contentPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._contentPane.verticalScrollPolicy = ScrollPolicy.ON;
            this._contentPane.width = 280;
            this._contentPane.height = 145;
            this._contentPane.move(0, 75);
            this.addChild(this._contentPane);
            this._contentText = ObjCreate.createGTextInput(0, 0, 260, 145);
            this._contentText.textField.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._contentText.editable = false;
            this._contentText.textField.multiline = true;
            this._contentText.textField.wordWrap = true;
            this._contentPane.source = this._contentText;
            this._contentText.addEventListener(flash.events.TextEvent.LINK, this.onLinkTextClickHandler);
            this._goodsLabel = new GLabel();
            this._goodsLabel.htmlText = HTMLUtil.addColor(Language.getString(52002), "#FFFFFF");
            this._goodsLabel.setSize(20, 50);
            this._goodsLabel.move(13, 223);
            this.addChild(this._goodsLabel);
            this.createAttachItem();
            this._goldBitmap = GlobalClass.getBitmap("Yuanbao");
            this._goldBitmap.x = 171;
            this._goldBitmap.y = 225;
            this.addChild(this._goldBitmap);
            this._goldText = ObjCreate.createGTextInput(196, 223, 88, 20, this);
            this._goldText.editable = false;
            this._goldText.name = "goldText";
            this._goldText.text = "0";
            this._coinBitmap = GlobalClass.getBitmap("Tongqian");
            this._coinBitmap.x = 171;
            this._coinBitmap.y = 245;
            this.addChild(this._coinBitmap);
            this._coinText = ObjCreate.createGTextInput(196, 245, 88, 20, this);
            this._coinText.editable = false;
            this._coinText.name = "coinText";
            this._coinText.text = "0";
            this._collectBtn = this.createGButton(6, 265, 55, 23, "CollectButton", 52003);
            this._writeBtn = this.createGButton(74, 265, 55, 23, "WriteButton", 52004);
            this._replyBtn = this.createGButton(140, 265, 45, 23, "ReplyButton", 52005);
            this._delBtn = this.createGButton(190, 265, 45, 23, "DelButton", 52006);
            return;
        }

        /* internal  */createGButton(arg1: number, arg2: number, arg3: int, arg4: int, arg5: string, arg6: int): GButton
        {
            var loc1;
            (loc1 = ObjCreate.createGButton("", arg1, arg2, arg3, arg4, this, "MailBtn")).name = arg5;
            loc1.label = "";
            var loc2=(arg4 - 20) / 2 + 2;
            var loc3;
            (loc3 = ObjCreate.createGTextField("", 0, loc2, arg3, 20, loc1)).textColor = 11727614;
            var loc4;
            (loc4 = loc3.defaultTextFormat).align = flash.text.TextFormatAlign.CENTER;
            loc3.defaultTextFormat = loc4;
            loc3.htmlText = Language.getString(arg6);
            return loc1;
        }

        /* internal */ /* var */_addresserLabel: GLabel;

        /* internal */ /* var */_addresserText: GTextInput;

        /* internal */ /* var */_typeBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_titleLabel: GLabel;

        /* internal */ /* var */_titleText: GTextInput;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_contentPane: GScrollPane;

        /* internal */ /* var */_contentText: GTextInput;

        /* internal */ /* var */_goodsLabel: GLabel;

        /* internal */ /* var */_goldBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_coinBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_goldText: GTextInput;

        /* internal */ /* var */_coinText: GTextInput;

        /* internal */ /* var */_collectBtn: GButton;

        /* internal */ /* var */_writeBtn: GButton;

        /* internal */ /* var */_replyBtn: GButton;

        /* internal */ /* var */_smail: SMail;

        /* internal */ /* var */_tipsLabel: GLabel;

        /* internal */ /* var */_itemsArray: Array<any>;

        /* internal */ /* var */_isItemTip: Boolean=false;

        /* internal */ /* var */_showContent: string="";

        /* internal */ /* var */_typeName: string="";

        /* internal */ /* var */_delBtn: GButton;
    }
